/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.core.date.DateTime;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.TemplateMsgConstant;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.MaintainPlanScoreRecord;
import org.springblade.modules.admin.service.WeChatDialogueService;
import org.springblade.modules.admin.vo.MaintainPlanScoreRecordVO;
import org.springblade.modules.admin.mapper.MaintainPlanScoreRecordMapper;
import org.springblade.modules.admin.service.IMaintainPlanScoreRecordService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 任务打分记录表 服务实现类
 *
 * @author ysq
 * @since 2022-09-22
 */
@Service
@Slf4j
@AllArgsConstructor
public class MaintainPlanScoreRecordServiceImpl extends BaseServiceImpl<MaintainPlanScoreRecordMapper, MaintainPlanScoreRecord> implements IMaintainPlanScoreRecordService {

	private final WeChatDialogueService weChatDialogueService;

	@Override
	public IPage<MaintainPlanScoreRecordVO> selectMaintainPlanScoreRecordPage(IPage<MaintainPlanScoreRecordVO> page, MaintainPlanScoreRecordVO maintainPlanScoreRecord) {
		return page.setRecords(baseMapper.selectMaintainPlanScoreRecordPage(page, maintainPlanScoreRecord));
	}

	@Override
	public void sendWechatTemplate(Date yesterday) {
		List<MaintainPlanScoreRecordVO> list = baseMapper.selectScoreRecordList(yesterday);
		// 根据用户分组
		Map<Long, List<MaintainPlanScoreRecordVO>> map = list.stream().collect(Collectors.groupingBy(t -> t.getUserId()));
		map.forEach((k, v) -> {
			List<LocalDateTime> orderDates = v.stream().map(t -> t.getOrderTime()).sorted().collect(Collectors.toList());
			List<LocalDateTime> finishDates = v.stream().map(t -> t.getFinishTime()).sorted().collect(Collectors.toList());
			// 跳转链接
			String skipUrl = String.format("pages/maintenanceStatistics/index?scoreState=1&startTime=%tF&endTime=%tF", yesterday, yesterday);
			Map<String, String> templateDataMap = new HashMap<>(8);
			templateDataMap.put("first","您的工单已完成-评分");
			templateDataMap.put("keyword1", String.format("%d个工单已评分", v.size()));
			templateDataMap.put("keyword2", String.format("%tF~%tF", orderDates.get(0), orderDates.get(orderDates.size() -1)));
			templateDataMap.put("keyword3", "维修保养");
			templateDataMap.put("keyword4", v.get(0).getRealName());
			templateDataMap.put("keyword5", String.format("%tF~%tF", finishDates.get(0), finishDates.get(orderDates.size() -1)));
			templateDataMap.put("remark", "请前往历史工单查看评分结果");
			templateDataMap.put("params",skipUrl);
			if (Func.isNotEmpty(v.get(0).getOpenid())) {
				log.info("===========发送模板消息-工单评价提醒,接收人:[{}]===========",  v.get(0).getRealName());
				weChatDialogueService.sendMsg(v.get(0).getOpenid(), TemplateMsgConstant.T_ORDER_SCORE_ID, templateDataMap);
			}
		});
	}

	@Override
	@TenantIgnore
	public void sendPlanScoreMessage(DateTime yesterday) {
		List<MaintainPlanScoreRecordVO> list = baseMapper.sendPlanScoreMessage(yesterday);
		// 根据用户分组
		Map<Long, List<MaintainPlanScoreRecordVO>> map = list.stream().collect(Collectors.groupingBy(t -> t.getUserId()));
		map.forEach((k, v) -> {
			List<LocalDateTime> orderDates = v.stream().map(t -> t.getOrderTime()).sorted().collect(Collectors.toList());
			List<LocalDateTime> finishDates = v.stream().map(t -> t.getFinishTime()).sorted().collect(Collectors.toList());
			// 跳转链接
			String skipUrl = String.format("subpages/workOrders/records?type=1&tabIdx=0&tabStateIdx=5", yesterday, yesterday);
			Map<String, String> templateDataMap = new HashMap<>(8);
			templateDataMap.put("first","工单评价提醒");
			templateDataMap.put("keyword1", String.format("%d个工单已评分", v.size()));
			templateDataMap.put("keyword2", String.format("%tF~%tF", orderDates.get(0), orderDates.get(orderDates.size() -1)));
			templateDataMap.put("keyword3", "保养");
			templateDataMap.put("keyword4", v.get(0).getRealName());
			templateDataMap.put("keyword5", String.format("%tF~%tF", finishDates.get(0), finishDates.get(orderDates.size() -1)));
			templateDataMap.put("remark", "请前往历史工单查看评分结果");
			templateDataMap.put("params",skipUrl);
			if (Func.isNotEmpty(v.get(0).getOpenid())) {
				log.info("===========发送模板消息-工单评价提醒,接收人:[{}]===========",  v.get(0).getRealName());
				weChatDialogueService.sendMsg(v.get(0).getOpenid(), TemplateMsgConstant.T_ORDER_SCORE_ID, templateDataMap);
			}
		});
	}
}
